﻿using Coldairarrow.Entity.Goods;
using Coldairarrow.Entity.Home;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Home
{
    public class BTB_NewProductsBusiness : BaseBusiness<BTB_NewProducts>, IBTB_NewProductsBusiness, ITransientDependency
    {
        readonly IOperator _operator;

        public BTB_NewProductsBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<BTB_NewProducts>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var result = GetIQueryable();
            var where = LinqHelper.True<BTB_NewProducts>();
            int count = result.Count();
            var q = await result.Select(s => new BTB_NewProducts
            {
                id = s.id,
                TopPic = s.TopPic,
                LeftPic = s.LeftPic,
                TopGoodsid = Db.GetIQueryable<BTB_Goods>(true).Where(z => z.ID == s.TopGoodsid).FirstOrDefault().GoodsName,
                LeftGoodsid = Db.GetIQueryable<BTB_Goods>(true).Where(z => z.ID == s.LeftGoodsid).FirstOrDefault().GoodsName,
                GoodsTypeid = Db.GetIQueryable<BTB_GoodsType>(true).Where(z=> z.id == s.GoodsTypeid).FirstOrDefault().TypeName,
                CreatorId = s.CreatorId,
                CreateTime = s.CreateTime,
                Deleted = s.Deleted,
                Editor = _operator.UserId,
                EditTime = s.EditTime

            }).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_NewProducts> { Data = q, Total = count };
        }

        public async Task<List< BTB_NewProducts>> GetTheDataAsync(string id)
        {
            var q = GetIQueryable().Where(z => z.id == id).Select(s => new BTB_NewProducts
            {
                id = s.id,
                TopPic = s.TopPic,
                LeftPic = s.LeftPic,
                GoodsTopName = Db.GetIQueryable<BTB_Goods>(true).Where(z => z.ID == s.TopGoodsid).FirstOrDefault().GoodsName,
                TopGoodsid = s.TopGoodsid,
                GoodsLeftName = Db.GetIQueryable<BTB_Goods>(true).Where(z => z.ID == s.LeftGoodsid).FirstOrDefault().GoodsName,
                LeftGoodsid = s.LeftGoodsid,
                GoodsTypeid = s.GoodsTypeid,
                GoodsTypeName = Db.GetIQueryable<BTB_GoodsType>(true).Where(z => z.id == s.GoodsTypeid).FirstOrDefault().TypeName,
                CreatorId = s.CreatorId,
                CreateTime = s.CreateTime,
                Deleted = s.Deleted,
                Editor = _operator.UserId,
                EditTime = s.EditTime

            }).ToListAsync();
            return  await q;
        }
        /// <summary>
        /// 顶部广告
        /// type == 0 ------顶部广告
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task AddDataAsync(BTB_NewProducts data)
        {
            var goodsid = GetIQueryable().Where(q => q.GoodsTypeid == data.GoodsTypeid);
            if (goodsid.Count() > 0)
            {
                throw new BusException("该广告位已存在", errorCode: -1);
            }
            else
            {
                BTB_NewProducts data1 = new BTB_NewProducts()
                {
                    id = GuidHelper.GenerateKey(),
                    TopPic = data.TopPic,
                    LeftPic = data.LeftPic,
                    TopGoodsid = data.TopGoodsid,
                    LeftGoodsid = data.LeftGoodsid,
                    GoodsTypeid = data.GoodsTypeid,
                    CreatorId = _operator.UserId,
                    CreateTime = data.CreateTime,
                    Deleted = false,
                    Editor = data.Editor,
                    EditTime = data.EditTime
                };
                await InsertAsync(data1);
            }
        }
        public async Task UpdateDataAsync(BTB_NewProducts data)
        {
            var goodsid = GetIQueryable().Where(q => q.GoodsTypeid == data.GoodsTypeid);
            if (goodsid.Count() > 0 && goodsid.FirstOrDefault().GoodsTypeid != data.GoodsTypeid)
            {
                throw new BusException("该广告位已存在", errorCode: -1);
            }
            else
            {
                BTB_NewProducts data1 = new BTB_NewProducts()
                {
                    id = data.id,
                    TopPic = data.TopPic,
                    LeftPic = data.LeftPic,
                    TopGoodsid = data.TopGoodsid,
                    LeftGoodsid = data.LeftGoodsid,
                    GoodsTypeid = data.GoodsTypeid,
                    CreatorId = data.CreatorId,
                    CreateTime = data.CreateTime,
                    Deleted = false,
                    Editor = _operator.UserId,
                    EditTime = data.EditTime
                };
                await UpdateAsync(data1);
            }
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}